# ============================================
# PowerShell UTF-8 编码配置脚本
# ============================================
# 用途：解决 PowerShell 中中文和 emoji 显示乱码问题
# 作者：AI Assistant
# 日期：2025-11-10

Write-Host "`n=== PowerShell UTF-8 编码配置 ===" -ForegroundColor Cyan

# ============================================
# 1. 配置 PowerShell Profile
# ============================================
Write-Host "`n📝 Step 1: 配置 PowerShell Profile" -ForegroundColor Yellow

$profileContent = @"
# ============================================
# PowerShell UTF-8 Encoding Configuration
# ============================================
# This ensures proper display of Chinese characters and emoji in PowerShell

# Set console output encoding to UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# Set console input encoding to UTF-8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8

# Set PowerShell output encoding to UTF-8
`$OutputEncoding = [System.Text.Encoding]::UTF8

# Set code page to UTF-8 (65001)
chcp 65001 | Out-Null

# Set environment variable for less (used by git)
`$env:LESSCHARSET = 'utf-8'

# Set environment variable for Git pager
`$env:LC_ALL = 'C.UTF-8'

Write-Host '✓ UTF-8 encoding configured' -ForegroundColor Green
"@

# 确保配置文件目录存在
$profileDir = Split-Path $PROFILE -Parent
if (-not (Test-Path $profileDir)) {
    New-Item -ItemType Directory -Path $profileDir -Force | Out-Null
    Write-Host "   ✓ 创建配置目录: $profileDir" -ForegroundColor Green
}

# 备份现有配置文件（如果存在）
if (Test-Path $PROFILE) {
    $backupPath = "$PROFILE.backup.$(Get-Date -Format 'yyyyMMdd-HHmmss')"
    Copy-Item $PROFILE $backupPath
    Write-Host "   ✓ 备份现有配置: $backupPath" -ForegroundColor Green
}

# 写入配置文件
$profileContent | Out-File -FilePath $PROFILE -Encoding UTF8 -Force
Write-Host "   ✓ 配置文件已创建: $PROFILE" -ForegroundColor Green

# ============================================
# 2. 配置 Git 编码
# ============================================
Write-Host "`n🔧 Step 2: 配置 Git 编码" -ForegroundColor Yellow

# Core settings
git config --global core.quotepath false
Write-Host "   ✓ core.quotepath = false (显示中文文件名)" -ForegroundColor Green

# Commit encoding
git config --global i18n.commitencoding utf-8
Write-Host "   ✓ i18n.commitencoding = utf-8 (提交消息编码)" -ForegroundColor Green

# Log output encoding
git config --global i18n.logoutputencoding utf-8
Write-Host "   ✓ i18n.logoutputencoding = utf-8 (日志输出编码)" -ForegroundColor Green

# GUI encoding
git config --global gui.encoding utf-8
Write-Host "   ✓ gui.encoding = utf-8 (GUI 编码)" -ForegroundColor Green

# ============================================
# 3. 验证配置
# ============================================
Write-Host "`n🔍 Step 3: 验证配置" -ForegroundColor Yellow

Write-Host "`n   📄 PowerShell Profile:"
Write-Host "      Location: $PROFILE"
Write-Host "      Exists: $(Test-Path $PROFILE)"

Write-Host "`n   🔧 Git Configuration:"
$gitConfigs = git config --global --get-regexp "^(core\.quotepath|i18n\.|gui\.encoding)"
if ($gitConfigs) {
    $gitConfigs | ForEach-Object {
        Write-Host "      $_" -ForegroundColor Gray
    }
}

# ============================================
# 4. 测试编码
# ============================================
Write-Host "`n🧪 Step 4: 测试编码" -ForegroundColor Yellow

Write-Host "   测试中文: 你好，世界！" -ForegroundColor Cyan
Write-Host "   测试 Emoji: ✅ ⚠️ 🚀 📸 🔄 ⏱️ 🎨" -ForegroundColor Cyan

# ============================================
# 完成
# ============================================
Write-Host "`n✅ 配置完成！" -ForegroundColor Green
Write-Host "`n⚠️  重要提示:" -ForegroundColor Yellow
Write-Host "   1. 请重启 PowerShell 或 VS Code 使配置生效" -ForegroundColor White
Write-Host "   2. 如果在 VS Code 中使用，建议在设置中添加:" -ForegroundColor White
Write-Host '      "terminal.integrated.defaultProfile.windows": "PowerShell"' -ForegroundColor Gray
Write-Host '      "terminal.integrated.profiles.windows": {' -ForegroundColor Gray
Write-Host '        "PowerShell": {' -ForegroundColor Gray
Write-Host '          "source": "PowerShell",' -ForegroundColor Gray
Write-Host '          "args": ["-NoExit", "-Command", "chcp 65001"]' -ForegroundColor Gray
Write-Host '        }' -ForegroundColor Gray
Write-Host '      }' -ForegroundColor Gray
Write-Host "   3. 对于 Git commit，推荐使用 Git Bash 或 VS Code 的 Source Control" -ForegroundColor White

Write-Host "`n📚 使用建议:" -ForegroundColor Cyan
Write-Host "   • PowerShell: 适合日常命令行操作" -ForegroundColor White
Write-Host "   • Git Bash: 推荐用于包含中文的 git commit" -ForegroundColor White
Write-Host "   • VS Code: 推荐用于代码编辑和 git 操作" -ForegroundColor White

Write-Host "`n按任意键退出..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

